package com.java2007.hotel.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/07
 **/
public class JdbcUtils {
    /**
     * 数据源
     */
    private static DruidDataSource dataSource = null;
    /**
     * 连接
     */
    private static Connection connection = null;
    /**
     * 连接锁
     */
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    static {
        InputStream in = null;
        Properties properties = new Properties();
        try {
            in = JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(in);
            //创建连接池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        return dataSource;
    }

    /**
     * 获取连接
     *
     * @return
     */
    public static Connection getConnection() {
        try {
            //从锁中获取连接
            connection = threadLocal.get();
            //判断链接是否为空
            if (connection == null) {

                //从连接池中获取连接
                connection = dataSource.getConnection();
                threadLocal.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }

    /**
     * 关闭连接
     *
     * @param connection
     */
    public static void release(Connection connection) {
        try {
            //判断链接是否为空
            if (connection != null) {
                connection.close();
                threadLocal.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("关闭连接失败");
        }
    }


    /**
     * 开启事务
     */
    public static void startTransactions() {
        try {
            JdbcUtils.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("开启事务失败");
        }
    }

    /**
     * 提交事务
     */
    public static void commit() {
        try {
            JdbcUtils.getConnection().commit();
            JdbcUtils.release(JdbcUtils.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("提交失败");
        }
    }

    /**
     * 回滚事务
     */
    public static void rollBack() {
        try {
            JdbcUtils.getConnection().rollback();
            JdbcUtils.release(JdbcUtils.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("回滚失败");
        }
    }


}
